package com.pi.uc.service;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.pi.base.enumerate.record.RecordState;
import com.pi.base.exception.ServiceException;
import com.pi.base.util.bean.ObjectTool;
import com.pi.uc.dao.entity.UcUserConfigEntity;
import com.pi.uc.dao.mapper.UcUserConfigMapper;
import com.pi.uc.dao.param.UcUserConfigParam;
import com.pi.uc.enumerate.UserConfigType;

@Validated
@Service
public class UcUserConfigService {
  @Autowired
  private UcUserConfigMapper userConfigMapper;
  
  public void addUserConfig(    
      @NotNull(message = "UC_CONFIG.USER_ID_EMPTY")Long userId, 
      @NotNull(message = "UC_CONFIG.USER_CINFIG_EMPTY")UserConfigType configName, 
      @NotNull(message = "UC_CONFIG.USER_CINFIG_EMPTY")String configValue){
    addUserConfig(userId, configName.getConfigName(), configValue);
  }
  
  public void addUserConfig(
      @NotNull(message = "UC_CONFIG.USER_ID_EMPTY")Long userId, 
      @NotNull(message = "UC_CONFIG.USER_CINFIG_EMPTY")String configName, 
      @NotNull(message = "UC_CONFIG.USER_CINFIG_EMPTY")String configValue){
    UcUserConfigEntity config = queryUserConfig(userId, configName);
    if(null != config){
      throw new ServiceException("UC_CONFIG.USER_CONFIG_EXISTS");
    }
    UcUserConfigEntity entity = new UcUserConfigEntity();
    entity.setUserId(userId);
    entity.setConfigName(configName);
    entity.setConfigValue(configValue);
    entity.setConfigState(RecordState.STATE_NORMAL.getCode());
    userConfigMapper.insert(entity);
  }
  public UcUserConfigEntity queryUserConfig(
      @NotNull(message = "UC_CONFIG.USER_ID_EMPTY")Long userId, 
      @NotNull(message = "UC_CONFIG.USER_CINFIG_EMPTY")UserConfigType configName){
    return queryUserConfig(userId, configName.getConfigName());
  }
  public UcUserConfigEntity queryUserConfig(
      @NotNull(message = "UC_CONFIG.USER_ID_EMPTY")Long userId, 
      @NotNull(message = "UC_CONFIG.USER_CINFIG_EMPTY")String configName){
    UcUserConfigParam param = new UcUserConfigParam();
    param.setUserId(userId);
    param.setConfigName(configName);
    param.setConfigState(RecordState.STATE_NORMAL.getCode());
    List<UcUserConfigEntity> configList = userConfigMapper.findList(param);
    return ObjectTool.getOne(configList);
  }
}
